/**
 * Application information and properties controller static object.
 * 
 * .platforms           : IPHONE; ANDROID; PC
 * .platform            : one of .platforms
 * .viewportHeight      : the height of the viewport
 * .viewportWidth       : the width of the viewport
 * .addResizeHandler    : adds a function to be invoked when the viewport is resized.
 */
function AppInfo()
{
	// Define platforms.
	AppInfo.platforms = {IPHONE: 0, ANDROID: 1, PC: 2};
	
	// Prepare array for resize handler functions.
	AppInfo.resizeHandlers = new Array();
	
	// Detect platform.
	AppInfo.detectPlatform();
	
	// Detect viewport.
	AppInfo.detectViewport();
	
	// Attach to rsize event.
	jQuery(window).resize(function(){AppInfo.onResize();});
}

/**
 * Sets up the AppInfo.platform static variable.
 */
AppInfo.detectPlatform = function()
{
	// Set platform identifier.
    if (navigator.userAgent.indexOf('iPhone') != -1)
    {
        // iPhone.
        AppInfo.platform = AppInfo.platforms.IPHONE;
    }
    else if (navigator.userAgent.indexOf('Android') != -1)
    {
        // Android.
        AppInfo.platform = AppInfo.platforms.ANDROID;
    }
    else
    {
        // Personal computer.
        AppInfo.platform = AppInfo.platforms.PC;
    }
};

/**
 * Sets up the AppInfo.viewportWidth and AppInfo.viewportHeight static variables.
 */
AppInfo.detectViewport = function()
{
	// Copied from (and modified): http://snipplr.com/view/5896/get-browser-viewport-width-and-height/
	// The more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerWidth != 'undefined')
    {
    	AppInfo.viewportWidth = window.innerWidth,
    	AppInfo.viewportHeight = window.innerHeight
    }
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    else if (typeof document.documentElement != 'undefined'
        && typeof document.documentElement.clientWidth != 'undefined'
        && document.documentElement.clientWidth != 0)
    {
        AppInfo.viewportWidth = document.documentElement.clientWidth,
        AppInfo.viewportHeight = document.documentElement.clientHeight
    }
    // older versions of IE
    else
    {
    	AppInfo.viewportWidth = jQuery('body')[0].clientWidth,
    	AppInfo.viewportHeight = jQuery('body')[0].clientHeight
    }
};

/**
 * Function to be called on window resize event.
 */
AppInfo.onResize = function()
{
	AppInfo.detectViewport();
	// Notify all registered resize handlers.
	for (var index=0; index<AppInfo.resizeHandlers.length; index++)
	{
		AppInfo.resizeHandlers[index]();
	}
};

/**
 * Adds a resize handler function.
 */
AppInfo.addResizeHandler = function(func)
{
	AppInfo.resizeHandlers[AppInfo.resizeHandlers.length] = func;
};